#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time : 2022-2-25 10:02
# @Author : huabao.tan
# @File : excel_tool.py
# @Software: PyCharm
import tkinter as tk
import os
from tkinter import filedialog
from openpyxl import load_workbook


class CreatExcel:
    def __init__(self):
        self.filename = self.open_file()
        self.workbook = self.open_excel()

    def open_file(self):
        root = tk.Tk()
        root.withdraw()
        Fpath = filedialog.askopenfilename()
        return Fpath


    def open_excel(self):
        if os.path.exists(self.filename):
            workbook = load_workbook(self.filename)

            return workbook

    def get_sheet_by_index(self, workbook, index):
        sheetnames = self.workbook.sheetnames
        sheet = self.workbook[sheetnames[index]]
        return sheet

    def sheet_row_col_value(self, index=None, row=None, col=None, value=None):

        # print( self.workbook)
        sheetnames = self.workbook.sheetnames
        sheet = self.workbook[sheetnames[index]]
        sheet.cell(row, col).value = value
        # print('成功写入')

    def save_file(self):
        self.workbook.save(self.filename)
        self.workbook.close()

if __name__ == '__main__':
    c = CreatExcel()
